package com.vanky.graph.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;

public class JsonUtil {
    public static <T> List<T> toJson(List<Object> data, Class<T> typeClass) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        ArrayList<T> result = new ArrayList<>();
        for (Object obj : data) {
            try {
                String jsonString = objectMapper.writeValueAsString(obj);
                T t = objectMapper.readValue(jsonString, typeClass);
                result.add(t);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
